package com.osexp.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.osexp.controller.PC;

public class MainPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jp1,jp2;
	private JRadioButton box1,box2,box3,box4;
	private JButton situation1,situation2,showTLB,showMM;
	public PC pc;
	protected int flag = 0;
	/**
	 *constructor 
	 */
	public MainPanel(){
		this.setLayout(null);
		//initial all components
		initComponent();
		
		System.out.println(box1.isSelected());
	}
	/**
	 *initial all components 
	 */
	public void initComponent(){
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		pc = new PC();
		TextArea text=new TextArea();
		text.setBounds(40, 40, 420, 420);
		text.setFont(new Font("黑体",Font.BOLD,14));
		text.setBackground(new Color(144, 238, 144));
		this.add(text);
		showTLB = new JButton("展示TLB状态");
		showTLB.setBorderPainted(false);
		showTLB.setBounds(500, 255, 180, 50);
		showTLB.addActionListener(new ActionListener(){	
			public void actionPerformed(ActionEvent e){
				String temp =pc.showTLB();
				text.setText(text.getText()+temp);
				text.setCaretPosition(text.getText().length());
				return;
			}
		});
		this.add(showTLB);
		
		showMM = new JButton("展示内存状态");
		showMM.setBorderPainted(false);
		showMM.setBounds(500, 180, 180, 50);
		showMM.addActionListener(new ActionListener(){	
			public void actionPerformed(ActionEvent e){
				String temp =pc.showMM();
				text.setText(text.getText()+temp);
				text.setCaretPosition(text.getText().length());
				return;
			}
		});
		this.add(showMM);

		situation1 = new JButton("执行一条");
		situation1.setBorderPainted(false);
		situation1.setBounds(500, 330, 180, 50);
		situation1.addActionListener(new ActionListener(){	 
			public void actionPerformed(ActionEvent e) {  
				flag = 0;
			if(box1.isSelected()&&box3.isSelected()){//判断radioButton是否被选中  	
				pc.setCurrReplacementAlgorithm(0);//0=FIFO	1=LRU
				pc.setCurrReplacementStrategy(0);//0=全局 	1=局部
				text.setText(text.getText()+OperationNextOne());
				return;
			}  
			else if(box1.isSelected()&&box4.isSelected()){ 
				pc.setCurrReplacementAlgorithm(0);//0=FIFO	1=LRU
				pc.setCurrReplacementStrategy(1);//0=全局 	1=局部
				text.setText(text.getText()+OperationNextOne());
				return;              
			}else if (box2.isSelected()&&box3.isSelected()){
				pc.setCurrReplacementAlgorithm(1);//0=FIFO	1=LRU
				pc.setCurrReplacementStrategy(0);//0=全局 	1=局部
				text.setText(text.getText()+OperationNextOne());
				return;
			}else if (box2.isSelected()&&box4.isSelected()){
				pc.setCurrReplacementAlgorithm(1);//0=FIFO	1=LRU
				pc.setCurrReplacementStrategy(1);//0=全局 	1=局部
				text.setText(text.getText()+OperationNextOne());
				return;
			} 

		} }); 
		this.add(situation1);
		
		situation2 = new JButton("执行十条");
		situation2.setBorderPainted(false);
		situation2.setBounds(500, 410, 180, 50);
		situation2.addActionListener(new ActionListener(){	 public void actionPerformed(ActionEvent e) {  
			if(e.getSource()==situation2){  
				flag = 1;
				//System.out.println("按钮被选中.....");  
				if(box1.isSelected()&&box3.isSelected()){//判断radioButton是否被选中  
					pc.setCurrReplacementAlgorithm(0);//0=FIFO	1=LRU
					pc.setCurrReplacementStrategy(0);//0=全局 	1=局部
					text.setText(text.getText()+OperationNextTen());
					return;
				}  
				else if(box1.isSelected()&&box4.isSelected()){
					pc.setCurrReplacementAlgorithm(0);//0=FIFO	1=LRU
					pc.setCurrReplacementStrategy(1);//0=全局 	1=局部
					text.setText(text.getText()+OperationNextTen());
					return;                  
				}else if (box2.isSelected()&&box3.isSelected()){
					pc.setCurrReplacementAlgorithm(1);//0=FIFO	1=LRU
					pc.setCurrReplacementStrategy(0);//0=全局 	1=局部
					text.setText(text.getText()+OperationNextTen());
					return;
				}else if (box2.isSelected()&&box4.isSelected()){
					pc.setCurrReplacementAlgorithm(1);//0=FIFO	1=LRU
					pc.setCurrReplacementStrategy(1);//0=全局 	1=局部
					text.setText(text.getText()+OperationNextTen());
					return;
				}  
			}  

		} });
		this.add(situation2);
		
		jp1=initialPanel("使用算法","FIFO","LRU");
		jp2=initialPanel("作用范围选择","全局","局部");
		jp1.setBounds(500, 40, 300, 50);
		this.add(jp1);
		jp2.setBounds(500, 90, 300, 100);
		this.add(jp2);
		
//		JLabel about = new JLabel("By");
//		about.setBounds(550,475,30,20);
//		this.add(about);
//		JLabel author = new JLabel("丁俊杰  姚睿宏 姚尔佳");
//		author.setBounds(580,480,150,80);
//		this.add(author);
	}

	/**
	 * initial a choose panel
	 */
	public JPanel initialPanel(String A,String B,String C) {
		JLabel jlb = new JLabel(A);
		jlb.setBounds(0, 0, 90, 50);
		JPanel panel =new JPanel();
		panel.setLayout(null);
		ButtonGroup bg = new ButtonGroup();
		panel.add(jlb);
		if(box1==null&&box2==null){
			box1 = new JRadioButton(B);
			box1.setBounds(90, 0, 60, 50);
			box2 = new JRadioButton(C);
			box2.setBounds(150, 0, 60, 50);
			box1.addActionListener(new ActionListener(){	
				public void actionPerformed(ActionEvent e){
					rest();
				}
			});box2.addActionListener(new ActionListener(){	
				public void actionPerformed(ActionEvent e){
					rest();
				}
			});
			bg.add(box1);
			bg.add(box2);
			panel.add(box1);
			panel.add(box2);
		}else{
			box3 = new JRadioButton(B);
			box3.setBounds(90, 0, 60, 50);
			box4 = new JRadioButton(C);
			box4.setBounds(150, 0, 60, 50);
			box3.addActionListener(new ActionListener(){	
				public void actionPerformed(ActionEvent e){
					rest();
				}
			});box4.addActionListener(new ActionListener(){	
				public void actionPerformed(ActionEvent e){
					rest();
				}
			});
			bg.add(box3);
			bg.add(box4);
			panel.add(box3);
			panel.add(box4);
		}
		
		return panel;
	}
	
	/**
	 *Operation for the next record
	 */
	public String OperationNextOne(){
		String temp = null;
		temp=pc.Next()+pc.currPrint();
		return temp;
	}
	/**
	 * Operation for the next ten records
	 */
	public String OperationNextTen(){
		String temp=null;
		for(int i=0;i<10;i++){
			temp=pc.Next()+pc.currPrint();
		}
		return temp;
	}
	/**
	 *初始化PC 
	 */
	private void rest() {
		// TODO Auto-generated method stub
		pc=new PC();
	}

    
}
